package cn.smvc.sys.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import cn.smvc.common.utils.StringUtils;
import cn.smvc.sys.dao.SysDictDao;
import cn.smvc.sys.dao.impl.SysDictDaoImpl;
import cn.smvc.sys.entity.SysDict;
import cn.smvc.sys.service.SysDictService;

@Service
public class SysDictServiceImpl implements SysDictService{
	
	private SysDictDao sysDictDao = new SysDictDaoImpl();

	@Override
	public List<SysDict> findAllType() throws SQLException {
		return sysDictDao.findAllType();
	}

	@Override
	public SysDict findOne(String id) throws SQLException {
		return sysDictDao.findOne(id);
	}

	@Override
	public int save(SysDict task) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 通过id删除数据
	 * @param id
	 * @return int
	 * @throws SQLException
	 */
	@Override
	public int delete(String id) throws SQLException {
		return sysDictDao.delete(id, 1);
	}

	@Override
	public int update(SysDict task) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> queryPage(Map<String, String[]> param) {
		int pageNo = Integer.parseInt(param.get("pageNo")[0].toString());
		int pageSize = Integer.parseInt(param.get("pageSize")[0].toString());
		String label = param.get("label") == null ? "" : param.get("label")[0];
		String type = param.get("type") == null ? "" : param.get("type")[0];
		String description = param.get("description") == null ? "" : param.get("description")[0];
		String remarks = param.get("remarks") == null ? "" : param.get("remarks")[0];
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		try {
			StringBuilder where = new StringBuilder();
			where.append("del_flag=0 ");
			if(StringUtils.isNotEmpty(label)) {
				where.append("and label like '%").append(label).append("%' ");
			}
			if(StringUtils.isNotEmpty(type)) {
				where.append("and type='").append(type).append("' ");
			}
			if(StringUtils.isNotEmpty(description)) {
				where.append("and description  like '%").append(description).append("%' ");
			}
			if(StringUtils.isNotEmpty(remarks)) {
				where.append("and remarks like '%").append(remarks).append("%' ");
			}
			List<SysDict> list = sysDictDao.findPage(where.toString(), pageNo, pageSize, "type, sort, create_date");
			count = sysDictDao.count(where.toString());
			map.put("code", 0);
			map.put("msg", "查询成功！");
			map.put("data", list);
			map.put("rows", count);//总行数
			map.put("pageNo", pageNo);
			map.put("pageSize", pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return map;
	}

}
